package com.gitee.cheungming.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cheungming.goods.dao.GoodsMapper;
import com.gitee.cheungming.goods.model.Goods;
import com.gitee.cheungming.goods.service.IGoodsService;
import io.seata.core.context.RootContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Zhang Ming
 * @since 2025-10-28
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Override
    @Transactional
    public Boolean reduceStock(Long id, Integer count) {
        String xid = RootContext.getXID();
        System.out.println("=== 库存服务收到XID: {} ===" + xid);
        System.out.println("=== 扣除商品{}库存,{}个 ===" + id + count);
        Goods goods = getById(id);
        if (goods.getStock() >= count) {
            goods.setStock(goods.getStock() - count);
            updateById(goods);
            return true;
        }
        throw new RuntimeException("库存不足");
    }
}
